package com.aliyun.heiyu.common.util;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class SSHUtil {
    private static Logger logger = LoggerFactory.getLogger(SSHUtil.class);

    private String ip;
    private int port;
    private String user;
    private String pa;
    private Session session;
    private ChannelExec channelExec;

    public SSHUtil(String ip, int port, String user, String pa) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pa = pa;
    }

    public void sessionConnect() {
        JSch jsch = new JSch();
        try {
            session = jsch.getSession(user, ip, port);
            //  跳过受检
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(pa);
            session.connect();
            logger.info("连接成功");
        } catch (JSchException e) {
            logger.error("SSH连接失败");
            e.printStackTrace();
        }

    }

    public String exeCommand(String command) {
        sessionConnect();
        BufferedReader br = null;
        InputStream in = null;
        try {
            channelExec = (ChannelExec) session.openChannel("exec");
            in = channelExec.getInputStream();
            channelExec.setCommand(command);
            channelExec.setErrStream(System.err);
            channelExec.connect();
            logger.info("SSH远程命令 执行");
            StringBuffer buffer = new StringBuffer();
            br = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line + "\n");
            }
            return buffer.toString();
        } catch (JSchException e) {
            logger.error("JSchException{}", e);
            e.printStackTrace();
        } catch (IOException e) {
            logger.error("IOException{}", e);
            e.printStackTrace();
        } catch (Exception e) {
            logger.error("Exception{}", e);
            e.printStackTrace();
        } finally {
            try {
                if(null != in){
                    in.close();
                }
                if(null != br){
                    br.close();
                }
                if(null != channelExec) {
                    channelExec.disconnect();
                }
                session.disconnect();
            } catch (Exception e) {
                logger.error("关闭资源失败4{}", e);
                e.printStackTrace();
            }
        }
        return null;
    }


    public List<String> exeCommand1(String command) {
        BufferedReader br = null;
        InputStream in = null;
        try {
            sessionConnect();
            channelExec = (ChannelExec) session.openChannel("exec");
            in = channelExec.getInputStream();
            channelExec.setCommand(command);
            channelExec.setErrStream(System.err);
            channelExec.connect();
            logger.info("SSH远程命令{执行");
            List<String> list = new ArrayList<>();
            br = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
            String line = null;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
            return list;
        } catch (JSchException e) {
            logger.error("JSchException{}", e);
            e.printStackTrace();
        } catch (IOException e) {
            logger.error("IOException{}", e);
            e.printStackTrace();
        } catch (Exception e) {
            logger.error("Exception{}", e);
            e.printStackTrace();
        } finally {
            try {
                if(null != in){
                    in.close();
                }
                if(null != br){
                    br.close();
                }
                if(null != channelExec) {
                    channelExec.disconnect();
                }
                session.disconnect();
            } catch (Exception e) {
                logger.error("关闭资源失败{}", e);
                e.printStackTrace();
            }
        }
        return null;
    }

}